import fetchUtil from "../utils/fetchUtil";
const preurl = 'http://node.7chejian.com'
// const preurl = 'http://127.0.0.1:7001'

const isBand = (callback) => {
    let openid = localStorage.getItem('openid');
    fetchUtil.commonRequest(preurl + '/api/jyxuser/isband',{openid:openid}).then((data) => {
        if (!data.auth){
            getWxUser(callback);
        }else{
            localStorage.setItem('token',data.token);
            callback(data.token);
        }
    })
};

const getWxUser = (callback) => {
    let openid = localStorage.getItem('openid');
    fetchUtil.commonRequest(preurl + '/api/wx/getUserInfo',{openid:openid})
    .then((data) => {
        let params = {};
        params.name = data.nickname;
        params.phone = '';
        params.headImg = data.headimgurl;
        params.openid = openid;
        params.wallet = 0;

        bandUser(params,callback);
    });
};

const bandUser = (params,callback) => {
    let openid = localStorage.getItem('openid');
    fetchUtil.commonRequest(preurl + '/api/jyxuser/band',params).then((data) => {
        localStorage.setItem('token',data.token);
        callback(data.token);
    })
};

export const getUserToken = (callback) => {
    isBand(callback);
}

export const getUserInfo = (token) => {
    // let token = localStorage.getItem('token');
    console.log(token);
    let params = {
        token:token,
    }
    return fetchUtil.commonRequest(preurl + '/api/jyxuser/userinfo',params);
}

export const getTradeList = (page) => {
    let token = localStorage.getItem('token');
    let params = {
        token:token,
        page:page,
    }
    return fetchUtil.commonRequest(preurl + '/api/trade/tradelist',params);
}

export const getEquipArea = (page) => {
    let params = {
        page:page,
        size:10
    }
    return fetchUtil.commonRequest(preurl + '/api/m/equiparea/list',params);
}


export const getEquipsByArea = (areaId) => {
    let params = {
        areaId:areaId,
    }
    return fetchUtil.commonRequest(preurl + '/api/m/equipment/listbyarea',params);
}

export const getEquipDetail = (equipNo) => {
    return fetchUtil.commonRequest(preurl + '/api/equipment/detail',{equipNo:equipNo});
}

export const tradeAdd = (params) => {
    return fetchUtil.commonRequest(preurl + '/api/trade/add',params);
}


export const dealEquip = (id) => {
    let token = localStorage.getItem('token');
    let params = {
        tradeId:id,
        token:token,
    }
    return fetchUtil.commonRequest(preurl + '/api/equipment/deal',params);
}

export const getEquipState = (equipNo) => {
    let params = {
        equipNo:equipNo,
    }
    return fetchUtil.commonRequest(preurl + '/api/equipment/state',params);
}

export const cardSetUser = (cardNum) => {
    let token = localStorage.getItem('token');
    let params = {
        cardNum:cardNum,
        token:token,
    }
    return fetchUtil.commonRequest(preurl + '/api/m/card/setUser',params);
}

export const getQrCardDetail = (id) => {
    let params = {
        id:id,
    }
    return fetchUtil.commonRequest(preurl + '/api/m/qrcard/detail',params);
}

export const getQrCardDetailWithState = (id,cardNo) => {
    let params = {
        id:id,
        cardNo:cardNo
    }
    return fetchUtil.commonRequest(preurl + '/api/m/qrcard/detailwithstate',params);
}

export const updateUserCardWithState = (id,cardNo) => {
    let token = localStorage.getItem('token');
    let params = {
        id:id,
        token:token,
        cardNo:cardNo
    }
    return fetchUtil.commonRequest(preurl + '/api/m/qrcard/updateUserCardWithState',params);
}


export const getQrCard = (id) => {
    let token = localStorage.getItem('token');
    let params = {
        id:id,
        token:token,
    }
    return fetchUtil.commonRequest(preurl + '/api/m/qrcard/get',params);
}
